<template>
  <div>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button type="primary" @click="handleCreate"> 新增 </a-button>
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              icon: 'clarity:note-edit-line',
              label: '修改',
              onClick: handleEdit.bind(null, record),
            },
            {
              icon: 'clarity:note-edit-line',
              label: '测试',
              onClick: handleTest.bind(null, record),
            },
            {
              icon: 'clarity:note-edit-line',
              label: '主配置',
              ifShow: () => {
                return !record.master;
              },
              onClick: handleMaster.bind(null, record),
            },
            {
              icon: 'clarity:note-edit-line',
              color: 'error',
              label: '删除',
              popConfirm: {
                title: '是否要删除数据？',
                placement: 'left',
                confirm: handleDelete.bind(null, record),
              },
            },
          ]"
        />
      </template>
    </BasicTable>
    <FileConfigModal @register="registerModal" @success="reload()" />
  </div>
</template>
<script lang="ts" setup>
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useModal } from '/@/components/Modal';
  import FileConfigModal from './FileConfigModal.vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import {
    getFileConfigList,
    deleteFileConfig,
    testFileConfig,
    updateFileConfigMaster,
  } from '/@/api/backs/file/fileConfig';
  import { columns, searchFormSchema } from './ficleConfig.data';

  defineOptions({ name: 'InfraFileConfig' });

  const { t } = useI18n();
  const { createConfirm, createMessage, createSuccessModal } = useMessage();
  const [registerModal, { openModal }] = useModal();

  const [registerTable, { reload }] = useTable({
    title: '文件配置列表',
    api: getFileConfigList,
    columns,
    formConfig: { labelWidth: 120, schemas: searchFormSchema },
    useSearchForm: true,
    showTableSetting: true,
    showIndexColumn: false,
    beforeFetch: (params) => {
      if (params.createTime) {
        params.startTime = params.createTime[0];
        params.endTime = params.createTime[1];
        delete params.createTime;
      }
    },
    actionColumn: {
      width: 280,
      title: '操作',
      dataIndex: 'action',
      slots: { customRender: 'action' },
      fixed: 'right',
    },
  });

  function handleCreate() {
    openModal(true, { isUpdate: false });
  }

  function handleEdit(record: Recordable) {
    openModal(true, { record, isUpdate: true });
  }

  async function handleTest(record: Recordable) {
    const res = await testFileConfig({ id: record.id });
    createSuccessModal({ content: res });
  }

  function handleMaster(record: Recordable) {
    createConfirm({
      title: '主配置',
      iconType: 'warning',
      content: '是否确认修改配置编号为"' + record.id + '"的数据项为主配置?',
      async onOk() {
        await updateFileConfigMaster({ id: record.id });
        createMessage.success('配置成功');
        reload();
      },
    });
  }

  async function handleDelete(record: Recordable) {
    await deleteFileConfig({ id: record.id });
    createMessage.success('删除成功！');
    reload();
  }
</script>
